package com.diyunapp.xilinapp.adapters;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.bean.OrderPayOutBean;
import com.diyunapp.xilinapp.details.GoodsDetailActivity;
import com.diyunapp.xilinapp.myapi.MyOrdersClickInterface;
import com.diyunapp.xilinapp.utils.DateUtils;
import com.diyunapp.xilinapp.utils.FormatConfirm;
import com.diyunapp.xilinapp.utils.ScrollListViewDisplay;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by NIDAYE on 2016/6/27.
 */
public class OrderPayOuterAdapter extends BaseAdapter implements View.OnClickListener {

    public List<OrderPayOutBean> mDatas;
    public Context myContext;
    private MyOrdersClickInterface clickInterface;

    public OrderPayOuterAdapter(List<OrderPayOutBean> mDatas, Context myContext, MyOrdersClickInterface clickInterface) {
        this.mDatas = mDatas;
        this.myContext = myContext;
        this.clickInterface = clickInterface;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.i("zhangxiaoxidebug", "position = " + position);
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(myContext).inflate(R.layout.orderpay_out_list, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final OrderPayOutBean currentdata = mDatas.get(position);
        holder.orderpayOrdernumTv.setText("订单号： " + currentdata.getOrder_sn());
        holder.orderpayStoreCb.setText(currentdata.getStoreName());

        final int state = FormatConfirm.stringToInt(currentdata.getOrder_state());
        int clickType = 0;
        final int evalu_state = FormatConfirm.stringToInt(currentdata.getEvaluation_state());
        String if_lock=currentdata.getIf_lock();
        if (currentdata.isDisplaybottom()) {
            switch (state) {
                case 0:  //已取消
                    holder.myordersButtonLl.setVisibility(View.VISIBLE);
                    holder.myordersPayBt.setVisibility(View.GONE);
                    holder.myordersCancelBt.setVisibility(View.VISIBLE);
                    holder.myordersCancelBt.setText("删除订单");
                    holder.myordersCancelBt.setTag(position);
                    holder.drawbackBt.setVisibility(View.GONE);
                    break;
                case 10:  //待付款
                    holder.myordersButtonLl.setVisibility(View.VISIBLE);
                    holder.myordersPayBt.setVisibility(View.GONE);
                    holder.myordersCancelBt.setVisibility(View.VISIBLE);
                    holder.myordersCancelBt.setText("取消订单");
                    holder.myordersCancelBt.setTag(position);
                    holder.drawbackBt.setVisibility(View.GONE);
                    break;
                case 20: //待发货
                    clickType = 1; //仅执行交易投诉
                    holder.myordersButtonLl.setVisibility(View.VISIBLE);
                    holder.myordersPayBt.setVisibility(View.GONE);
                    holder.myordersCancelBt.setVisibility(View.GONE);
                    holder.drawbackBt.setVisibility(View.VISIBLE);
                    if(if_lock=="true"||if_lock.equals("true"))
                    {
                        holder.drawbackBt.setText("退款退货中");
                        holder.drawbackBt.setTag(position);
                    }
                    else //可执行交易投诉和退款退货
                    {
                        holder.drawbackBt.setText("退款退货");
                        holder.drawbackBt.setTag(position);
                    }
                    break;
                case 30://待收货
                    holder.myordersButtonLl.setVisibility(View.VISIBLE);
                    holder.myordersPayBt.setVisibility(View.VISIBLE);
                    holder.myordersCancelBt.setVisibility(View.VISIBLE);
                    holder.myordersCancelBt.setText("查看物流");
                    holder.myordersCancelBt.setTag(position);
                    holder.myordersPayBt.setText("确认收货");
                    holder.myordersPayBt.setTag(position);
                    holder.drawbackBt.setVisibility(View.GONE);
                    if(if_lock=="true"||if_lock.equals("true"))
                    {
                        clickType = 3; //可执行交易投诉和退款状态
                    }
                    else //可执行交易投诉和退款退货
                    {
                        clickType = 2;
                    }
                    break;
                case 40://已收货
                    clickType = 4; //可执行交易投诉和退款退货
                    if (evalu_state < 1) {  //待评价
                        holder.myordersButtonLl.setVisibility(View.VISIBLE);
                        holder.myordersPayBt.setVisibility(View.VISIBLE);
                        holder.myordersCancelBt.setVisibility(View.GONE);
                        holder.myordersPayBt.setText("立即评价");
                        holder.myordersPayBt.setTag(position);
                        holder.drawbackBt.setVisibility(View.GONE);
                        if(if_lock=="true"||if_lock.equals("true"))
                        {
                            clickType = 3; //可执行交易投诉和退款状态
                        }
                        else //可执行交易投诉和退款退货
                        {
                            clickType = 4;
                        }
                    } else {  //已评价
                        holder.myordersButtonLl.setVisibility(View.VISIBLE);
                        holder.myordersCancelBt.setVisibility(View.VISIBLE);
                        holder.myordersCancelBt.setText("删除订单");
                        holder.myordersCancelBt.setTag(position);
                        holder.myordersPayBt.setVisibility(View.GONE);
                        holder.drawbackBt.setVisibility(View.VISIBLE);
                        holder.drawbackBt.setText("追加评论");
                        holder.drawbackBt.setTag(position);
                    }
                    break;
                default:
                    break;
            }
        } else {
            holder.myordersButtonLl.setVisibility(View.GONE);
        }

        OrderInnerAdapter innerAdapter = new OrderInnerAdapter(currentdata.getGoodsList(), myContext,clickType,currentdata.getOrder_id());
        holder.orderpayGoodslistLv.setAdapter(innerAdapter);
        holder.orderpayGoodslistLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent detailInt = new Intent(myContext, GoodsDetailActivity.class);
                detailInt.putExtra("GoodId", currentdata.getGoodsList().get(position).getGoodId());
                myContext.startActivity(detailInt);
            }
        });
        innerAdapter.notifyDataSetChanged();
        ScrollListViewDisplay.setListViewHeightBasedOnChildren(holder.orderpayGoodslistLv);
        Long ordertime = Long.parseLong(currentdata.getAdd_time());
        holder.orderpayTimeTv.setText("下单时间：" + DateUtils.getCompleteTime(ordertime));
        holder.orderpayPaytypeTv.setText(currentdata.getPayment_name());
        holder.orderpayStateTv.setText(currentdata.getState_desc());

        holder.orderpayYunfeeTv.setText("运费：￥" + currentdata.getShipping_fee());
        holder.orderpayGoodsTv.setText("商品总价：￥" + currentdata.getGoods_amount());
        float xlb = FormatConfirm.stringToFloat(currentdata.getXlb_amount());
        if (xlb > 0) {
            holder.orderpayXlbTv.setText("希霖币抵扣：￥-" + currentdata.getXlb_amount());
            holder.orderpayXlbTv.setVisibility(View.VISIBLE);
        } else {
            holder.orderpayXlbTv.setVisibility(View.GONE);
        }

        float yhq = FormatConfirm.stringToFloat(currentdata.getDjj());
        if (yhq > 0) {
            holder.orderpayYhqTv.setText("代金券抵扣：￥-" + currentdata.getDjj());
            holder.orderpayYhqTv.setVisibility(View.VISIBLE);
        } else {
            holder.orderpayYhqTv.setVisibility(View.GONE);
        }
        if(currentdata.getStore_mansong().isEmpty())
        {
            holder.orderpayMansongTv.setVisibility(View.GONE);
        }
        else
        {
            holder.orderpayMansongTv.setVisibility(View.VISIBLE);
            holder.orderpayMansongTv.setText("活动: "+currentdata.getStore_mansong());
        }

        holder.orderpayTotalTv.setText("本店总价：￥" + currentdata.getOrder_amount());
        float chargecard = FormatConfirm.stringToFloat(currentdata.getRcb_amount());
        if (chargecard > 0) {
            holder.orderpayChargecradTv.setText("充值卡支付：￥" + currentdata.getRcb_amount());
            holder.orderpayChargecradTv.setVisibility(View.VISIBLE);
        } else {
            holder.orderpayChargecradTv.setVisibility(View.GONE);
        }

        float account = FormatConfirm.stringToFloat(currentdata.getPd_amount());
        if (account > 0) {
            holder.orderpayAccountTv.setText("预付款支付：￥" + currentdata.getPd_amount());
            holder.orderpayAccountTv.setVisibility(View.VISIBLE);
        } else {
            holder.orderpayAccountTv.setVisibility(View.GONE);
        }
        holder.orderStoretotalMoneyTv.setText("￥" + currentdata.getPay_money());


        holder.myordersPayBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickInterface.OnOrdersClick(v, state, currentdata.getOrder_id());
            }
        });

        holder.myordersCancelBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickInterface.OnOrdersClick(v, state, currentdata.getOrder_id());
            }
        });

        final String s=holder.drawbackBt.getText().toString().trim();
        holder.drawbackBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(s=="退款退货中"||s.equals("退款退货中"))
                {
                    Toast.makeText(myContext, "您已申请了退款退货，查看进度请到“退款及退货”列表！", Toast.LENGTH_LONG).show();
                 }
                else
                {
                    clickInterface.OnOrdersClick(v, state, currentdata.getOrder_id());
                }
            }
        });

        return convertView;
    }

    @Override
    public void onClick(View v) {

    }


    static class ViewHolder {
        @Bind(R.id.orderpay_ordernum_tv)
        TextView orderpayOrdernumTv;
        @Bind(R.id.orderpay_store_cb)
        CheckBox orderpayStoreCb;
        @Bind(R.id.orderpay_goodslist_lv)
        ListView orderpayGoodslistLv;
        @Bind(R.id.orderpay_time_tv)
        TextView orderpayTimeTv;
        @Bind(R.id.orderpay_paytype_tv)
        TextView orderpayPaytypeTv;
        @Bind(R.id.orderpay_state_tv)
        TextView orderpayStateTv;
        @Bind(R.id.orderpay_yunfee_tv)
        TextView orderpayYunfeeTv;
        @Bind(R.id.orderpay_goods_tv)
        TextView orderpayGoodsTv;
        @Bind(R.id.orderpay_xlb_tv)
        TextView orderpayXlbTv;
        @Bind(R.id.orderpay_yhq_tv)
        TextView orderpayYhqTv;
        @Bind(R.id.orderpay_mansong_tv)
        TextView orderpayMansongTv;
        @Bind(R.id.orderpay_total_tv)
        TextView orderpayTotalTv;
        @Bind(R.id.orderpay_chargecrad_tv)
        TextView orderpayChargecradTv;
        @Bind(R.id.orderpay_account_tv)
        TextView orderpayAccountTv;
        @Bind(R.id.order_storetotal_money_tv)
        TextView orderStoretotalMoneyTv;
        @Bind(R.id.orderpay_ordermoney_ll)
        LinearLayout orderpayOrdermoneyLl;
        @Bind(R.id.myorders_cancel_bt)
        TextView myordersCancelBt;
        @Bind(R.id.myorders_pay_bt)
        TextView myordersPayBt;
        @Bind(R.id.drawback_bt)
        TextView drawbackBt;
        @Bind(R.id.myorders_button_ll)
        LinearLayout myordersButtonLl;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
